package com.zige.zige.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.zige.zige.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by song on 2015/4/23.
 */
public class LittlezebraDB extends SQLiteOpenHelper {

    private static final String DB_NAME = "littlezebra.db";
    private static int db_version = 1;
    private Context context;

    private static LittlezebraDB db;

    public static SQLiteOpenHelper getInstance(Context context) {
        if (db == null)
            db = new LittlezebraDB(context);
        return db;
    }

    private LittlezebraDB(Context context) {
        this(context, DB_NAME, null, db_version);
        this.context = context;
    }

    public LittlezebraDB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table Province(_id integer primary key autoincrement,Province text)");
        db.execSQL("create table City(_id integer primary key autoincrement,City text,Province_id integer)");
        BufferedReader br = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(R.raw.place)));
        String len ;
        try {
            while (null != (len = br.readLine())) {
                db.execSQL(len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
